#模拟银行系统
#功能:账户注册,root模式,密码保护,查询,存款,取款,贷款,投资,转账,改密
#更新:简易,功能加了一点
import json 
#文件保存在data.json(附带data-raw.json初始备份改名就行)
import os
import time
import calendar
from msvcrt import getwch#仅Windows平台

file="data.json"
with open(file,"r") as data: #读入数据
    w=json.loads(data.read()) #数据都在这里Wallet
admit = False #验证是否进入主界面
while True:
    #登入界面
    os.system('cls')
    usr=input("输入用户名(0退出银行):")
    if usr == "0":
        break
    elif usr == "root" and input()=="root":#root模式
        input(w)
        continue
    #如果此用户不存在,创建用户并设密码,并回到登入界面
    elif usr not in w:
        w[usr]=[input("新用户请设置密码:"),0,[]]
        continue
    #如果存在,进入验证界面
    for times in [4,3,2,1]:
        if w[usr][0] == input(f"输入'{usr}'的密码:"):
            os.system('cls')
            print("登录成功!")
            admit = True
            break #退出验证循环
        else:
            print(f'密码不对!还有{times}次!')
    else:
        print(f"错误过多!退出'{usr}'的登录!")
    while admit:
        mid=list(time.localtime(time.time()))
        print(calendar.month(mid[0],mid[1])) #输出日历
        tim=time.strftime("%Y-%m-%d %H:%M",time.localtime()) #输出当前时间
        print("客户:",usr,tim)
        print(f"财产:{w[usr][1]}")
        print("1.存取款 2.年贷 3.投资 4.转账 g.日志 x.账户 0.退出:")
        opt = getwch()
        os.system('cls')
        while opt == "1": #存取款
            try:
                much = int(input("输入金额(带负号为取款):"))
            except ValueError:
                print("输入异常")
            else:
                w[usr][1] += much
                w[usr][2].append(f"{tim} {much:+}")
                break
        while opt == "2": #年贷
            try:
                loan=int(input("贷款:"))
                rate=1+float(input("年利率(不带百分号的小数):"))
                pay=int(input("每年还款:"))
            except ValueError:
                print("输入异常!")
            else:
                if min(loan,rate,pay)<=0 or (loan-pay)*rate>=loan:
                    print("输入的数据有误")
                    continue
                sum=0
                while loan >0:
                    print(f"{loan:.2f},支付{min(pay,loan):.2f}")
                    loan=(loan-min(pay,loan))*rate
                    sum+=min(pay,loan)
                sum=int(sum)
                w[usr][1]-=sum
                w[usr][2].append(f"{tim} Loan {sum}")
                break
        while opt == "3": #投资
            try:
                inv = 12*int(input("每月定投金额:"))
                rate = 1+float(input("年收益率:"))
                year = int(input("定投期限(年):"))
            except ValueError:
                print("输入异常!")
            else:
                if min(inv,rate,year)<=0:
                    print("输入的数据有误")
                    continue
                expected = inv*(rate**year-1)
                print(f"定投的预期收入为:{expected:.2f}" )
                w[usr][2].append(f"{tim} Invest {expected:.2f}")
                break
        while opt=="4": #转账
            try:
                towho=input("输入要转给的用户名：")
                much=int(input("输入金额："))
            except:
                print("输入异常！")
            else:
                if towho not in w:
                    continue
                w[usr][1]-=much
                w[usr][2].append(f"{tim} Carry {much} to {towho}")
                w[towho][1]+=much
                w[towho][2].append(f"{tim} Carry {much} from {usr}")
                break
        if opt=="g": #日志
            for log in w[usr][2]:
                print(log)
            if input("继续(输入 del 清除日志)")=="del":
                w[usr][2]=[]
        if opt == "x": #账户
            os.system("cls")
            print("1.修改密码 2.删除账号 3.实名验证(开发中)")
            flag = getwch()
            if flag == "1" and w[usr][0] == input("输入原来密码:"):
                w[usr][0]=input("输入新的密码:")
            elif flag == "2":
                w.pop(usr)
            admit = False
        elif opt == "0": #退出
            admit = False
with open(file,"w") as data:
    data.write(json.dumps(w)) #保存数据